<?php

	/**
	* @package ParkFind
	* @desc This is the search park script php file. This script will generate the sql query that will be run to look for the park.
	* 		 This script uses the parameters given in the find.php file.
	*/
	
	
	  // Direct calling check
	if (!isset($_GET["name"]))
		die("Do not call this page directly");
	
	session_start();
	/**
	* @desc This include file will load the directory settings for the Smarty Templates
	*/
   	require 'includes/smarty.inc';
	$smarty->assign('title', "Results");
	$smarty->assign('user_id', $_SESSION["user_id"]);	
	$smarty->assign('last', $_SESSION["lastviewed"]);	
	$smarty->assign('firstname', $_SESSION["firstname"]);
	$smarty->assign('role', $_SESSION["role"]);
	$smarty->assign('top5', $_SESSION["top5"]);	
	$smarty->display('header.tpl');
   
		
	/**
	* @desc This include file connects to the database.
	*/
	require 'includes/connect.inc';
	
	// Store variables, escape them aswell
	$name = mysql_real_escape_string($_GET["name"], $connection);
	$type = mysql_real_escape_string($_GET["type"], $connection);
	$postcode = mysql_real_escape_string($_GET["postcode"], $connection);
	$region = mysql_real_escape_string($_GET["region"], $connection);
	$page = $_GET["page"];
	
	$smarty->assign('name', $name); // Assign comment limit so it only shows a few at a time
	$smarty->assign('region', $region); // Assign comment limit so it only shows a few at a time
	$smarty->assign('postcode', $postcode); // Assign comment limit so it only shows a few at a time
	$smarty->assign('type', $type); // Assign comment limit so it only shows a few at a time
		
		
	// Make arrays
	$_SESSION["errors"] = array();
	$_SESSION["form"] = array();

	$conditions = NULL; // Define variable
	
	// Name validation		
	If (!empty($name))
	{	
		if (!preg_match('/^[a-zA-Z ]+$/', $name))
			$_SESSION["errors"]["name"] = "Only letters allowed";
			
		else if (strlen($name) > 40)
			$_SESSION["errors"]["name"] = "Maximum 40 characters";
			
		else	
			$_SESSION["form"]["name"] = $name; // Store variable incase error
			addCondition("parks.name LIKE '%" . $name . "%'");	// Add condition to SQL
	}
	
	// If there were any errors, show the page again
	if (count($_SESSION["errors"]))
	{
		mysql_close($connection); // Close connection
		header("Location: find.php");
		exit;
	}
	
	
	// Park Type
	if ($type != "<Any>")	
		addCondition("parks.type = '" . $type ."'");	// Add condition to SQL
	
	
	// Postcode
	if ($postcode != "<Any>")	
		addCondition("suburbs.postcode = " . $postcode);	// Add condition to SQL
	
	// Regions
	if ($region != "<Any>")	
		addCondition("regions.name LIKE '%" . $region . "%'");	// Add condition to SQL
	
	
	$selects = "parks.park_id, suburbs.name AS 'subname', suburbs.postcode, parks.type, parks.name, parks.address";
	
	$froms = "parks JOIN suburbs ON ( parks.suburb_id = suburbs.suburb_id ) " .
			"JOIN regions ON ( regions.region_id = suburbs.region_id )";
	
	// Build query
	$query = "SELECT ".$selects." FROM ".$froms.$conditions; //." GROUP BY wine_id, state ORDER BY wine_id";				
	
	// If page isnt set (ie first time results are shown), set this to 1
	if (!$page)
		$page = 1;
	
	/**
	* @desc This include file will load the functions that are required to call 
	*		from the database
	*/
	require 'includes/db_functions.inc';
	
	$parks = getParksPage($page);
	
	$arParks = array();
	
	if ($parks)
	{
		// Iterate park
		while ($row = mysql_fetch_array($parks))
		{
			switch ($row["type"])
			{
				case 'Skate Parks' :
					$icon = "skate.png";
					break;
				case 'Garden and Reserve' :
					$icon = "park.png";
					break;					
				case 'Gardening Club' :
					$icon = "club.png";
					break;
					
				case 'Recreation Group' :
					$icon = "club.png";
					break;
				default :
					$icon = "default.png";
					break;
			}
			
			$arParks[] = array('id' => $row["park_id"],
								'icon' => $icon,
								'subname' => $row["subname"],
								'postcode' => $row["postcode"],
								'type' => $row["type"],
								'name' => $row["name"],
								'address' => $row["address"]);
		
		}
	}
	
	
	$smarty->assign('prevpage', $page - 1); // Assign comment limit so it only shows a few at a time
	$smarty->assign('nextpage', $page + 1); // Assign comment limit so it only shows a few at a time
	
	$smarty->assign('count', getResultsCount($query)); // Assign count of results
	$smarty->assign('parks', $arParks); // Assign array even if empty
	
	$smarty->display('parkresults.tpl');
	
	$smarty->display('footer.tpl');
	
	mysql_close($connection); // Close connection
	
	// End of procedural script
	
	
	// **************** Functions *******************	
	/**
	 *@desc This function creates and combines any additional parameters that the user defines in the search page.
	 *@param string $cond
	 */
	function addCondition($cond) 
	{			
		global $conditions;
		
		if ($conditions == NULL) // If first time
			$conditions = " WHERE ".$cond;
		else
			$conditions = $conditions." AND ".$cond;				
	}
	
	/**
	 *@desc This function loads the data and creates the park page.
	 *@param string $page
	 *@return mixed
	 */
	function getParksPage($page)
	{
		global $park_id; // Make accessible in here
		global $query;  // Make accessible in here
		
		$page--; // 1 Bound
		
		$parks = getParks($query, ($page * 10), 10);
		return $parks;
	}
	
	
?>
